<template>
  <div class="box" shadow="never">
    <el-form ref="form" :model="User" label-width="300px" style="margin: 50px">
      <el-form-item label-width="100px" label="用户ID:">
        <el-input v-model="User.user_id" style="width: 400px"> </el-input>
      </el-form-item>

      <el-form-item label-width="100px" label="账户:">
        <el-input v-model="User.account" style="width: 400px"> </el-input>
      </el-form-item>

      <el-form-item label-width="100px" label="电话:">
        <el-input v-model="User.phone" style="width: 400px"> </el-input>
      </el-form-item>

      <el-form-item label-width="100px" label="邮箱:">
        <el-input v-model="User.email" style="width: 400px" />
      </el-form-item>

      <el-form-item label-width="100px" label="标签:">
        <el-input v-model="User.label" style="width: 400px" />
      </el-form-item>

      <el-form-item label-width="100px" label="密码:">
        <el-input v-model="User.password" style="width: 400px" />
      </el-form-item>

      <el-form-item label-width="100px" label="昵称:">
        <el-input v-model="User.nick_name" style="width: 400px" />
      </el-form-item>

      <el-form-item label-width="100px" label="真实姓名:">
        <el-input v-model="User.real_name" style="width: 400px" />
      </el-form-item>

      <el-form-item label-width="100px" label="人物头像:">
        <el-upload
          class="avatar-uploader"
          action="http://42.194.159.131:8080/pic/imgUpload?path=headpic"
          :headers="headers"
          :show-file-list="true"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img
            v-if="User.head_picture"
            :src="User.head_picture"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          <div slot="tip" class="el-upload__tip">
            只能修改图片为jpg/png文件，且不超过500kb
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item style="margin-left: -20px">
        <el-button type="primary" @click="onSubmit">编辑</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
export default {
  name: "User",
  data() {
    return {
      User: {},
      headers: {
	        'Authorization':getToken()
	      }
    };
  },
  created() {
    this.fatchDataId();
  },
  methods: {
    fatchDataId() {
      var id = this.$route.params.user_id;
      var vm = this;
      this.axios({
        method: "GET",
        url: "/user?user_id=" + id,
      }).then(function (resp) {
        vm.User = resp.data.data.user;
      });
    },
    onSubmit() {
      var vm = this;
      this.axios({
        method: "PUT",
        url: "/user",
        data: vm.User,
      })
        .then(function (resp) {
          vm.$router.go(-1);
          if (resp.data.errorCode == 0) {
            vm.$message({
              message: "修改成功!",
              type: "success",
            });
          }
        })
        .catch(function (error) {
          vm.$message.error("修改失败");
        });
    },
      //上传图片相关回调
    handleAvatarSuccess(res, file) {
        this.User.head_picture = res.data.picUrl;
        console.log("上传图片成功",res)
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;
         console.log("beforeAvatarUpload",file)
        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;

      }
    // -------------------------------------------------
  },
};
</script>

<style scoped>
.line {
  text-align: center;
}
.box {
  margin-left: 200px;
  height: 1100px;
  width: 700px;
  border: solid rgb(187, 186, 187);
}
 .avatar-uploader .el-upload {
    border: 3px dashed #decfcf;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>

